﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace Dao
{
    public class UserRatingDao
    {
        DBConnection dbConnect;
        public UserRatingDao()
        {
            dbConnect = new DBConnection();
        }

        public DataTable getAllUserRating()
        {
            string sql = "select * from UserRating";
            return dbConnect.executeSelectQuery(sql);
        }


        public DataTable getUserRatingById(int id)
        {
            string sql = "select * from UserRating where rateId =" + id + "";
            return dbConnect.executeSelectQuery(sql);
        }

        public bool insertUserRatingy(UserRating userRating)
        {
            string query = string.Format(@"insert into UserRating(itemId , buyerId) 
                                          values (@itemId , @buyerId)");
            SqlParameter[] sqlParameters = new SqlParameter[2];

            sqlParameters[0] = new SqlParameter("@itemId", SqlDbType.Int);
            sqlParameters[0].Value = Convert.ToString(userRating.ItemId);
            sqlParameters[1] = new SqlParameter("@buyerId", SqlDbType.Int);
            sqlParameters[1].Value = Convert.ToString(userRating.BuyerId);

            return dbConnect.executeInsertQuery(query, sqlParameters);
        }

        public bool updateUserRating(UserRating userRating)
        {
            string query = string.Format(@"update UserRating set itemId =@itemId, buyerId =@buyerId 
                                                  where rateId =@rateId");
            SqlParameter[] sqlParameters = new SqlParameter[3];

            sqlParameters[0] = new SqlParameter("@itemId", SqlDbType.Int);
            sqlParameters[0].Value = Convert.ToString(userRating.ItemId);

            sqlParameters[1] = new SqlParameter("@buyerId", SqlDbType.Int);
            sqlParameters[1].Value = Convert.ToString(userRating.BuyerId);

            sqlParameters[2] = new SqlParameter("@rateId", SqlDbType.Int);
            sqlParameters[2].Value = Convert.ToString(userRating.BuyerId);

            return dbConnect.executeInsertQuery(query, sqlParameters);
        }
    }
}
